package com.atyoung.youngmall.product.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.atyoung.youngmall.product.entity.AttrAttrgroupRelationEntity;
import com.atyoung.youngmall.product.entity.AttrEntity;
import com.atyoung.youngmall.product.entity.CategoryEntity;
import com.atyoung.youngmall.product.service.AttrAttrgroupRelationService;
import com.atyoung.youngmall.product.service.AttrService;
import com.atyoung.youngmall.product.service.CategoryService;
import com.atyoung.youngmall.product.vo.AttrGroupWithAttrVo;
import com.atyoung.youngmall.product.vo.AttrRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atyoung.youngmall.product.entity.AttrGroupEntity;
import com.atyoung.youngmall.product.service.AttrGroupService;
import com.atyoung.common.utils.PageUtils;
import com.atyoung.common.utils.R;



/**
 * 属性分组
 *
 * @author wen_chuang
 * @email wen_chuang@gmail.com
 * @date 2020-05-06 22:10:06
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
   // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catId") Long catId){
        PageUtils page = attrGroupService.queryPage(params,catId);

        return R.ok().put("page", page);
    }
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrRelationVo> attrRelationVos){
        attrAttrgroupRelationService.saveBatch(attrRelationVos);
        return R.ok();
    }

    //获取分类下所有分组&关联属性
    @GetMapping("/{catelogId}/withattr")
    public R  getAttrGroupWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrVo> list = attrGroupService.getAttrGroupWithAttrByCatelogId(catelogId);
        return R.ok().put("data",list);
    }

    //获取已经关联的属性
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> list = attrGroupService.queryAttrRelation(attrgroupId);
        return R.ok().put("data",list);
    }
    //获取没有关联的属性
    @GetMapping("{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String,Object> params,@PathVariable("attrgroupId") Long attrgroupId){
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    /**
     * 信息
     */
    @RequestMapping(name = "/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<Long> list = categoryService.getCategoryTreeNode(attrGroup.getCatelogId());
        Collections.sort(list);
        attrGroup.setCatelogPath(list);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody List<AttrRelationVo> attrRelationVo){
        attrGroupService.deleteAttrRelation(attrRelationVo);
        return R.ok();
    }

}
